package com.xlabs.server.DO;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.xlabs.client.DTO.ReplyDTO;

import javax.jdo.annotations.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Reply Data object.
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ReplyDO {

    /**
     * Primary key.
     */
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    /**
     * The text comment.
     */
    @Persistent
    private String comment;

    /**
     * The video that has this comment.
     */
    @Persistent
    private Key video;

    /**
     * Replies to this comment.
     */
    @Persistent
    private List<Key> replies;

    /**
     * The question for this reply.
     */
    @Persistent
    private Key replyTo;

    /**
     * The parent comment. Is not null for all comments other than the root questions.
     */
    @Persistent
    private Key originalQuestion;

    /**
     * When created.
     */
    @Persistent
    private Date createdOn;

    /**
     * Created by user.
     */
    @Persistent
    private User createdBy;

    /**
     * Initializes an instance of TimedComment.
     *
     * @param comment The comment.
     */
    public ReplyDO(String comment, Date createdOn) {
        this.comment = comment;
        this.createdOn = createdOn;
    }

    /**
     * Gets the key.
     *
     * @return id.
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     * @param key key.
     */
    public void setKey(Key key)
    {
        this.key = key;
    }

    /**
     * Gets comment.
     *
     * @return comment.
     */
    public String getReplyText() {
        return comment;
    }

    /**
     * Sets Comment.
     *
     * @param comment The text comment.
     */
    public void setComment(String comment) {
        this.comment = comment;
    }

    /**
     * Gets video.
     *
     * @return video.
     */
    public Key getVideo() {
        return video;
    }

    /**
     * Sets video
     *
     * @param video video that has this comment.
     */
    public void setVideo(Key video) {
        this.video = video;
    }

    /**
     * Gets replies.
     * @return List of replies.
     */
    public List<Key> getReplies() {
        return replies;
    }

    /**
     * Sets replies.
     * @param replies List of replies.
     */
    public void setReplies(List<Key> replies) {
        this.replies = replies;
    }

    /**
     * Adds a reply.
     * @param replyKey reply key.
     */
    public void addReply(Key replyKey)
    {
        if (this.replies == null)
        {
            this.replies = new ArrayList<Key>();
        }
        this.replies.add(replyKey);
    }

    /**
     * Gets the parent comment.
     * @return parent comment.
     */
    public Key getOriginalQuestion() {
        return originalQuestion;
    }

    /**
     * Sets relatedTo.
     * @param originalQuestion The original question to whose reply tree this reply blongs.
     */
    public void setOriginalQuestion(Key originalQuestion) {
        this.originalQuestion = originalQuestion;
    }

    /**
     * Returns the encoded Key of this jdo as a string.
     * @return encoded key.
     */
    public String getEncodedKey() {
        return KeyFactory.keyToString(this.key);
    }

    /**
     * Gets replyTo
     * @return The key of the reply to which this is a reply to.
     */
    public Key getReplyTo() {
        return replyTo;
    }

    /**
     * Sets replyTo.
     * @param replyTo Question to which this is a reply.
     */
    public void setReplyTo(Key replyTo) {
        this.replyTo = replyTo;
    }

    /**
     * Gets createdOn.
     * @return time of creation.
     */
    public Date getCreatedOn() {
        return createdOn;
    }

    /**
     * Sets time of creation.
     * @param createdOn time of creation.
     */
    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    /**
     * Gets createdBy.
     * @return creator.
     */
    public User getCreatedBy() {
        return createdBy;
    }

    /**
     * Sets createdBy.
     * @param createdBy creator.
     */
    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    /**
     * Converts an array of CommentDO objects to an array of TimedCommentDTO.
     * @param allReplyDOs
     * @return
     */
    public static ReplyDTO[] toReplyDTOArray(List<ReplyDO> allReplyDOs) {
        ReplyDTO[] replies = new ReplyDTO[allReplyDOs.size()];

        int index = 0;
        for (ReplyDO aReplyDO : allReplyDOs) {
            replies[index++] = aReplyDO.toReplyDTO();
        }

        return replies;
    }

    /**
     * Converts this objecto to CommentDTO.
     * @return com.xlabs.client.CommentDTO
     */
    public ReplyDTO toReplyDTO() {
        ReplyDTO dto = new ReplyDTO();

        dto.setReplyText(this.getReplyText());
        dto.setEncodedKey(this.getEncodedKey());
        dto.setCreatedBy(this.getCreatedBy().getNickname());
        dto.setCreatedOn(this.getCreatedOn());

        return dto;
    }
}